﻿using System;
using Alserve.Ffl.Data;


public partial class ffl_WebUserControls_register : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void RegisterNewUserButton_OnClick(object sender, EventArgs e)
    {

        if (ValidateRegistration())
        {
            User thisUser = new User();

            thisUser.AddNew();
            thisUser.Email = EmailTextBox.Text.Trim();
            thisUser.Forename = ForenameTextBox.Text.Trim();
            thisUser.Surname = SurnameTextBox.Text.Trim();
            thisUser.TeamName = TeamNameTextBox.Text.Trim();
            thisUser.Password = PasswordTextBox.Text.Trim();
            thisUser.Paid = false;
            thisUser.Admin = false;
            thisUser.Save();

            RegisterNewUserTable.Visible = false;
            SuccessLabel.Text = ForenameTextBox.Text.Trim() + ", you have successfully registered a new account. Please login using the menu.";
            SuccessLabel.Visible = true;
        }
    }

    private Boolean ValidateRegistration()
    {
        Boolean returnValue = true;
        ErrorTextLabel.Text = "";
        if (ForenameTextBox.Text.Length == 0)
        {
            ErrorTextLabel.Text += "Please enter a forename.<br/>";
            returnValue = false;
        }

        if (SurnameTextBox.Text.Length == 0)
        {
            ErrorTextLabel.Text += "Please enter a surname.<br/>";
            returnValue = false;
        }

        if ((EmailTextBox.Text.Length < 6) || (EmailTextBox.Text.IndexOf("@") == -1))
        {
            ErrorTextLabel.Text += "Please enter a valid email address.<br/>";
            returnValue = false;
        }

        if (PasswordTextBox.Text.Length < 6)
        {
            ErrorTextLabel.Text += "Password must be at least 6 characters.<br/>";
            returnValue = false;
        }

        if (PasswordTextBox.Text != ConfirmPasswordTextBox.Text)
        {
            ErrorTextLabel.Text += "Passwords do not match.<br/>";
            returnValue = false;
        }

        if (TeamNameTextBox.Text.Length == 0)
        {
            ErrorTextLabel.Text += "Please enter a team name.</br>";
            returnValue = false;
        }

        if (returnValue)
        {
            User thisUser = new User();
            thisUser.Where.Email.Value = EmailTextBox.Text.Trim();

            if (thisUser.Query.Load())
            {
                ErrorTextLabel.Text += "That email address has already been registered";
                returnValue = false;
            }
        }

        return returnValue;
    }
}

